
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutterroom/pages/my/data/room_peizhi.dart';
import 'package:flutterroom/pages/my/widget/add_room_img.dart';
import 'package:flutterroom/pages/my/widget/my_add_room_title.dart';
import 'package:flutterroom/pages/my/widget/my_add_room_title_list.dart';
import 'package:flutterroom/pages/my/widget/room_select_form_item.dart';

class MyAddRoom extends StatefulWidget {
  @override
  _MyAddRoomState createState() => _MyAddRoomState();
}

Widget _titleWidget(String name, BuildContext context) {
  return Expanded(
      child: Container(
    height: 49,
    child: GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        Navigator.of(context).pushNamed("login");
      },
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text(
            name,
            style: TextStyle(fontSize: 16),
          ),
          Icon(Icons.keyboard_arrow_right),
        ],
      ),
    ),
  ));
}

class _MyAddRoomState extends State<MyAddRoom> {
  int rentType =0;
  int decorationType =0;

  int roomType =0;
  int floor =0;
  int orientd =0;
  
  TextEditingController titleController;
  TextEditingController descController;
 

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: GestureDetector(
        onTap: () {
          // Navigator.of(context).pushNamed("roomAdd");
        },
        child: Container(
          padding: EdgeInsets.only(left: 10, right: 10, bottom: 6),
          width: ScreenUtil().setWidth(750) - 30,
          height: 44,
          decoration: BoxDecoration(
              color: Colors.green, borderRadius: BorderRadius.circular(5)),
          child: Center(
              child: Text(
            "发布",
            style: TextStyle(
                color: Colors.white, fontSize: 16, fontWeight: FontWeight.w600),
          )),
        ),
      ),
      appBar: AppBar(
        title: Text("发布房源"),
      ),
      body: ListView(
        children: <Widget>[
          AddRoomTitle(title: "房源信息"),
          RoomPriceItemWidget(
            label: "小区",
            contentBuilder: (context) {
              return _titleWidget("请选择小区", context);
            },
          ),
          RoomPriceItemWidget(
            label: "租金",
            hintText: "请输入租金",
            suffixText: "元/月",
          ),
          RoomPriceItemWidget(
            label: "大小",
            hintText: "请输入房屋大小",
            suffixText: "平方米",
          ),
          CommonRadioFormItem(label:"租赁方式",value: rentType,options: ["合租","整租"],onChange:(index){
            setState(() {
              rentType =index;
            });
          }),
          RoomSelectFormItem(
            label:"户型",
            options: ["一室","二室","三室","四室","五室","六室"],
            value: roomType,
            onChange: (index){
              setState(() {
                roomType =index;
              });
            },
          ),
          RoomSelectFormItem(
            label:"楼层",
            options: ["底层","中层","高层"],
            value: floor,
            onChange: (index){
              setState(() {
                floor =index;
              });
            },
          ),
          RoomSelectFormItem(
            label:"朝向",
            options: ["东","南","西","北"],
            value: orientd,
            onChange: (index){
              setState(() {
                orientd =index;
              });
            },
          ),


          CommonRadioFormItem(label:"装修",value: decorationType,options: ["精装","简装"],onChange:(index){
            setState(() {
              decorationType =index;
            });
          }),
          AddRoomTitle(title: "房源头像"),
          RoomImagePicker(
            onCheng: (List<File> files){

            },
          ),
          AddRoomTitle(title: "房源标题"),
          RoomTitleWidget(hitText: "请输入房屋标题",maxLines: 1,controller: titleController,),
          AddRoomTitle(title: "房源配置"),
          RoomAppliance(onChanged: (data){

          },),
          AddRoomTitle(title: "房源描述"),
          RoomTitleWidget(hitText: "请输入房屋描述",maxLines: 10,controller: descController,),
          // Text("测试一下"), 
        ],
      ),
    );
  }
}

class CommonRadioFormItem extends StatelessWidget {
  final String label;
  final List<String> options;
  final int value;
  final ValueChanged<int> onChange;

  const CommonRadioFormItem(
      {Key key, this.label, this.options, this.value, this.onChange})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return RoomPriceItemWidget(
      label: label,
      contentBuilder: (context) {
        return Container(
          child: Row(
            children: List.generate(options.length, (int index)=>
              Row(
                children: <Widget>[
                  Radio(value: index, groupValue: value, onChanged: onChange,hoverColor: Colors.green,),
                  Text(options[index]),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}


class RoomTitleWidget extends StatelessWidget {
  final String hitText;
  final TextEditingController controller;
  final int maxLines;

  const RoomTitleWidget({Key key, this.hitText, this.controller,this.maxLines}) : super(key: key);
 
  @override
  Widget build(BuildContext context) {
    return Container(
      // margin: EdgeInsets.only(bottom: 100),
      padding: EdgeInsets.only(left: 10,right: 10,bottom: 2),
      child: TextField(
        maxLines: maxLines,
        decoration: InputDecoration(
          border: InputBorder.none,
          hintText: hitText
        ),
      ),
    );
  }
}